package com.sidianzhong.sdz;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.alibaba.fastjson.JSONObject;


public class Test {

    public static final int phone = 10;

    private static final String url = "http://192.168.31.95:8080";
//    private static final String url = "http://noahglobal.me/NoahWallet";

    public static void main(String[] args) {
        for (int i = 0; i < phone; i++) {
            String s = String.valueOf(i);
            if (s.length() == 1) {
                s = "00" + s;
            } else if (s.length() == 2) {
                s = "0" + s;
            }
            register(s);
        }
    }

    private static void register(String nowPhone) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", "15100000" + nowPhone);
        String json = post(url + "/registerTest", map);
        System.out.println(json);
    }

    private static void shop(String nowPhone) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", "15100000" + nowPhone);
        String json = post(url + "/miningTest", map);
        System.out.println(json);
    }

    public static String post(String url, Map<String, Object> param) {
        StringBuilder builder = new StringBuilder();
        try {
            StringBuilder params = new StringBuilder();
            for (Map.Entry<String, Object> entry : param.entrySet()) {
                params.append(entry.getKey());
                params.append("=");
                params.append(entry.getValue().toString());
                params.append("&");
            }
            if (params.length() > 0) {
                params.deleteCharAt(params.lastIndexOf("&"));
            }
            URL restServiceURL = new URL(url + (params.length() > 0 ? "?" + params.toString() : ""));
            HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setUseCaches(false);
            httpConnection.setDoInput(true);
            httpConnection.setDoOutput(true);
            httpConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");//json格式
            httpConnection.setRequestProperty("Accept-Language", "zh-CN,zh");

            httpConnection.connect();


            OutputStream outputStream = httpConnection.getOutputStream();
            outputStream.write(params.toString().getBytes());
            outputStream.flush();
            outputStream.close();

            BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
            String str;
            while ((str = reader.readLine()) != null) {
                builder.append(str);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return builder.toString();
    }
}


